package com.mf.page;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.mf.MyApplication;

public class Actions {
	
	static String getResultString(Object result)
	{
		if( result != null && result.getClass().getSimpleName().equals("String") )
		{
			return (String) result;
		}
		return null;
		
	}
	
	/*
	 * 输入金额
	 * @return 返回输入的金额，若异常或取消，返回-1
	 */
	public static long GetAmount()	{
		long ret = -1;
		
		Bundle param = new Bundle();
		
		param.putInt("type",  InputTextActivity.Input_Amount );
		param.putString("title","");
		param.putString("tip","请输入金额");
		param.putString("rightBtnText","确定");
		param.putInt("maxlength",  12);
		param.putInt("minlength",  1);
		InputPageCallback inputCallback = new InputPageCallback();
		ShowBlockPage(param, InputTextActivity.class.getName(),inputCallback);
		ret = inputCallback.GetExitReason();
		if(inputCallback != null && inputCallback.GetExitReason() == IPageCallback.OK)
		{
			Object inputObject = inputCallback.GetInputText();
			
			if(inputObject != null)
			{
				String strAmount = getResultString(inputObject);
				if( strAmount != null )
				{
					try
					{
						double lastamount = Double.parseDouble(strAmount);					
						ret =  (long)(lastamount * 1000);
						ret = ret /10;
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		}
		
		return ret;
	}	

	/*
	 * 显示蓝牙设置界面
	 */
	public static void ShowBluetoothSetPage()
	{
		InputPageCallback inputCallback = new InputPageCallback();
		Actions.ShowBlockPage(null, BluetoothSetPage.class.getName(), inputCallback);
	}
	
	//提示界面是否被创建
	static boolean msgPageOpen = false;
	/*
	 * 显示一个非阻塞的提示界面。如果当前已经开启，则只改变当前界面的提示内容。如果未开启，则创建一个
	 * 注意：要调用CloseMsg来关闭
	 * 
	 */
	public static void ShowUnBlockMsg(String title,String msg, String lBtnText, String rBtnText, IPageCallback callbackProc)
	{
		Log.v("uninpay", "ShowUnBlockMsg, msgPageOpen:" + msg);
		//如果未打开，则打开一个提示界面
		if(!msgPageOpen)
		{
			msgPageOpen = true;
			Intent intent = new Intent(MyApplication.getInstance().getApplicationContext(), MsgPage.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Bundle param = new Bundle();
			param.putString("title",title);
			param.putString("msg",msg);
			param.putString("okBtn",null);
			param.putString("leftBtnText",lBtnText);
			param.putString("rightBtnText",rBtnText);
			param.putSerializable("pageCallback", callbackProc);
			intent.putExtras(param);
			MyApplication.getInstance().startActivity(intent);
		}
		else
		{	//已打开的发送广播更新内容
			Intent intent = new Intent(MsgPage.COMMPAGE_UPDATE_ACTION);
			Bundle param = new Bundle();
			param.putString("title",title);
			param.putString("msg",msg);
			param.putString("leftBtnText",lBtnText);
			param.putString("rightBtnText",rBtnText);
			param.putSerializable("pageCallback", callbackProc);
			intent.putExtras(param);
			
			MyApplication.getInstance().sendBroadcast(intent);
		}
	}
	public static void ShowUnBlockMsg(String title,String msg, String rBtnText, IPageCallback callbackProc)
	{
		Log.v("uninpay", msg);
		ShowUnBlockMsg(title, msg, "", rBtnText, callbackProc);
	}
	/*
	 * 显示一个阻塞的提示界面。如果当前已经开启，则只改变当前界面的提示内容。如果未开启，则创建一个
	 * 注意：要调用CloseMsg来关闭。按按钮不会主动关闭
	 * 
	 */
	public static void ShowBlockMsg(String title,String msg,String lBtnText, String rBtnText, IPageCallback btnProc)
	{
		try {
			Thread.sleep(300);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ShowUnBlockMsg(title, msg, lBtnText, rBtnText, btnProc);
		
		if(btnProc != null)
		{
			//等待按键按下的消息
			while(!btnProc.IsExit())
			{
				try{
					Thread.sleep(100);
				}catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					break;
				}
			}
		}
	}
	/*
	 * 显示一个阻塞的提示界面。如果当前已经开启，则只改变当前界面的提示内容。如果未开启，则创建一个
	 * 注意：要调用CloseMsg来关闭。按按钮不会主动关闭
	 * @return 左键返回IPageCallback.CANCEL, 超时返回IPageCallback.TIME_OUT,右键返回IPageCallback.OK
	 */
	public static int ShowBlockMsg(String title,String msg, String lBtnText, String rBtnText)
	{
		MsgPageCallback callback = new MsgPageCallback();
		ShowBlockMsg(title, msg, lBtnText,rBtnText, callback);

		return callback.GetExitReason();
	}
	/*
	 * 关闭由ShowUnBlockMsg和ShowBlockMsg创建的提示界面
	 */
	public static void CloseMsg()
	{
		if(msgPageOpen)
		{
			msgPageOpen = false;
			Intent intent = new Intent(MsgPage.COMMPAGE_CLOSE_ACTION);
			MyApplication.getInstance().sendBroadcast(intent);
		}
	}
	
	/*
	 * 显示一个指定的阻塞界面
	 * @param param 			[IN] 保存要在界面显示的参数，比如标题，内容，按钮文本
	 * @param pageClassName		[IN] 要显示的界面类名
	 */
	public static void ShowBlockPage(Bundle param, String pageClassName,IPageCallback callBack)
	{
		try{
			if(Class.forName(pageClassName) == null)
			{
				Log.v("uninpay", "class for name null");
			}
			if(MyApplication.getInstance().getApplicationContext() == null)
			{
				Log.v("uninpay", "context null");
			}
			Intent intent = new Intent(MyApplication.getInstance().getApplicationContext(), Class.forName(pageClassName));
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			
			if(param == null)
			{
				param = new Bundle();
			}
			
			param.putSerializable("pageCallback", callBack);
			intent.putExtras(param);
			MyApplication.getInstance().startActivity(intent);
			
			
			if(callBack != null)
			{
				//等待按键按下的消息
				while(!callBack.IsExit())
				{
					try{
						Thread.sleep(100);
					}catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						break;
					}
				}
			}
		}
		catch (ClassNotFoundException e) {
			
		}
	}
	
	static Handler handlers = new Handler() ;
}
